<template>
	<view class="content">

		<view class="box">
			<view class="title">
				请选择您的性别
			</view>

			<view class="item">
				<view class="items" v-for="(item,index) in list" :key="index" @tap="choose(index)">
					<view class="items_img">
						<image :class="[chooseIndex==index?'choose':'']" :src="item.image" mode=""></image>
					</view>
					<view class="inconer">
						<i class="iconfont icongouxuan icons" v-if="chooseIndex==index"></i>
						{{ item.text }}
					</view>
				</view>

			</view>

			<view class="next">
				<text @tap="next">下一步</text>
			</view>

		</view>




	</view>
</template>

<script>
	import {
		mapState,
		mapActions
	} from 'vuex'
	export default {
		computed: {
			...mapState(['token'])
		},
		data() {
			return {
				pages: null,
				chooseIndex: null,
				gender: null,
				list: [{
						image: "https://friend.tanshishang.cn/assets/img/boy.png",
						text: "男生"
					},
					{
						image: "https://friend.tanshishang.cn/assets/img/girl.png",
						text: "女生"
					},
				]
			};
		},
		onLoad(options) {
			//扫码进来的
			if (options.pages) {
				this.pages = true;
			}
		},
		onShow() {
			// uni.showModal({
			// 	title: '温馨提示',
			// 	content: '请您先选择性别!',
			// 	showCancel: false,
			// 	confirmText: '知道了',
			// 	success: (res) => {
			// 		console.log(res);
			// 	}
			// });
		},
		methods: {
			//点击选择
			choose(e) {
				// console.log(e);
				if (e == 0) {
					this.gender = 1;
				} else {
					this.gender = 2;
				}
				this.chooseIndex = e;
			},
			//点击下一步
			next() {
				console.log(this.chooseIndex);
				if (!this.chooseIndex && this.chooseIndex == null) {
					uni.showToast({
						title: '请先选择性别!',
						icon: 'none',
						mask: true
					})

				} else {
					//执行修改
					this.upInfo();
				}
			},
			async upInfo() {
				try {
					let parmars = {
						token: this.token,
						gender: this.gender
					}
					let res = await this.$request.request.get('/api/user/changeSex', parmars);
					console.log(res);
					if (res.code == 200) {
						uni.showToast({
							title: '设置成功!',
							mask: true
						});
						setTimeout(()=> {
							if(this.pages){
								uni.switchTab({
									url:'/pages/index/index'
								})
							}else{
								uni.navigateBack({
									delta:3
								})
							}
						},1000)
					}
				} catch (err) {
					this.$request.error(err, this)
				}
			}
		}
	}
</script>

<style lang="scss">
	@import 'chooseGender.scss'
</style>
